package com.karson.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.karson.common.constants.Constants;
import com.karson.common.model.AlbumComment;
import com.karson.common.model.User;
import com.karson.common.request.AlbumSearchRequest;
import com.karson.common.request.CommentRequest;
import com.karson.common.request.PageParamRequest;
import com.karson.common.response.*;
import com.karson.common.vo.MemberVo;
import com.karson.service.service.AlbumCommentService;
import com.karson.service.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Member;
import java.util.List;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/comment")
@Tag(name = "评论管理接口")
public class CommentController {
    @Autowired
    private AlbumCommentService albumCommentService;
    @Autowired
    private UserService userService;

    @Operation(summary = "分页列表")
    @GetMapping("/list/{albumId}")
    public CommonResult<RepliesResponse> getList(@RequestParam(required = false) String offset, @PathVariable Integer albumId, @RequestParam(required = false) Integer orderType) throws JsonProcessingException {
        if (albumId == null) {
            return CommonResult.failed();
        }
        RepliesResponse repliesResponse = albumCommentService.pageList(offset, albumId, orderType);
        return CommonResult.success(repliesResponse);
    }

    @Operation(summary = "获取子评论分页列表")
    @GetMapping("/getSubList/{albumId}")
    public CommonResult<IPage<CommentResponse>> getSubList(@PathVariable Integer albumId, @RequestParam String rId, @RequestParam(required = false) Integer orderType, @RequestParam Integer current) {
        if (rId == null) {
            return CommonResult.failed();
        }
        IPage<CommentResponse> page = new Page<>(current, Constants.PAGE_SMALL_SIZE);
        IPage<CommentResponse> pageList = albumCommentService.getPageList(page,albumId,orderType,rId);
        return CommonResult.success(pageList);
    }
    @Operation(summary = "添加评论")
    @PostMapping("/add")
    public CommonResult<CommentResponse> addComment(@RequestBody CommentRequest request) throws JsonProcessingException {
        CommentResponse commentResponse = albumCommentService.addComment(request);
        return CommonResult.success(commentResponse);
    }
    @Operation(summary = "删除评论")
    @DeleteMapping("/del/{id}")
    public CommonResult<CommentResponse> delComment(@PathVariable Integer id){
        if (id == null || id <= 0) {
            return CommonResult.validateFailed();
        }
        Boolean flag = albumCommentService.delComment(id);
        return flag ? CommonResult.success("ok") : CommonResult.failed();
    }
    @Operation(summary = "评论表头数量")
    @GetMapping("/tabs/headers")
    public CommonResult<List<CommentTabsHeader>> getTabsHeaders(){
        return CommonResult.success(albumCommentService.getTabsHeader());
    }
    @Operation(summary = "分页列表")
    @GetMapping("/commentList")
    public CommonResult<IPage<CommentAlbumResponse>> getList(@ModelAttribute @ParameterObject PageParamRequest pageParamRequest, @RequestParam(required = false) Integer albumId,@RequestParam Integer status) {
        IPage<CommentAlbumResponse> commentPage = new Page<>(pageParamRequest.getCurrent(), pageParamRequest.getSize());
        IPage<CommentAlbumResponse> commentPageInfo =  albumCommentService.getList(commentPage,albumId,status);
        return CommonResult.success(commentPageInfo);
    }
    @Operation(summary = "根据评论id查询")
    @GetMapping("/getRootCommentById")
    public CommonResult<CommentResponse> getRootCommentById(@RequestParam Integer id){
        AlbumComment comment = albumCommentService.getById(id);
        CommentResponse commentResponse = BeanUtil.copyProperties(comment, CommentResponse.class);
        if (comment == null) {
            return CommonResult.failed();
        }
        Integer userId = comment.getUserId();
        User user = userService.getById(userId);
        MemberVo member = BeanUtil.copyProperties(user, MemberVo.class);
        commentResponse.setMember(member);
        return CommonResult.success(commentResponse);
    }
    @Operation(summary = "修改评论状态")
    @PostMapping("/update")
    public CommonResult<String> updateComment(@RequestParam Integer id, @RequestParam Integer status) {
        LambdaUpdateWrapper<AlbumComment> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AlbumComment::getId, id);
        updateWrapper.set(AlbumComment::getStatus, status);
        albumCommentService.update(updateWrapper);
        return CommonResult.success("ok");
    }

}
